﻿using AutoMapper;
using B.R.RabbitRoot.Domain.animal_husbandry;
using B.R.RabbitRoot.ErrorCode;
using B.R.RabbitRoot.Infrastructure.Interface;
using B.R.RabbitRoot.Write.API.Applications.Commands.FarmingCommands;
using MediatR;

namespace B.R.RabbitRoot.Write.API.Applications.Commandhandlers.FarmingCommandHandlers
{
    /// <summary>
    /// 养殖信息添加命令
    /// </summary>
    public class FarmingCreamCommandHandler : IRequestHandler<FarmingCreamCommand, APIResult<int>>
    {
        private readonly IBaserepository<FarmingDetails> _farmingDetails;
        private readonly IMapper mapper;

        public FarmingCreamCommandHandler(IBaserepository<FarmingDetails> farmingDetails, IMapper mapper)
        {
            _farmingDetails = farmingDetails;
            this.mapper = mapper;
        }

        public async Task<APIResult<int>> Handle(FarmingCreamCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result=new APIResult<int>();
            result.Code=ResultCode.添加成功;
            result.Message = ResultCode.添加成功.ToString();
            var entity= mapper.Map<FarmingDetails>(request);
            result.Data = await _farmingDetails.AddAsync(entity);
            return await Task.FromResult(result);
        }
    }
}
